In this tutorial, we will discuss Python Object Oriented Programming concept and learn its fundamentals.
What are OOPs in Python?
As we know python is a high-level programming language with multi-paradigm, here Object-Oriented Programming is one of those paradigms. Object-oriented programming is a programming technique that most of the high-level programming language support in this we use class and object to make a program . An Object has two characteristics:
- Attribute
- Behaviours
Here attributes are the variable and behaviour can be taken as the function of those variables.
Let’s understand it with an example
Consider a Car as an Object So, its color, brand name, considered as its attributes Where the gear mechanism and speed would be considered as its behaviour.
OOP’s Properties
- Inheritance
When we use the properties of one class in another class.
- Encapsulation
All the code is inside a Class block.
- Polymorphism
Operators performing different operations on different data types
Two Main Component of OPP’s
- Class
- Object
Class
A class is a Keyword which is used to make the blueprint of an Object. A class has no existence until its object gets created.
Class Syntax
Example:
Object
The object of a class is also known as the instance of the class. When we create an object of the class the class come in existence, and with the help of object, we can access the properties of the class. A class has many objects.
Object creation syntax
Example
Suppose Bob and Sam has same car, and if we represent it with OOP’s concept the car would be a class and Bob and Sam will be Objects.
# Output
Behind the code
Here in the above code, we made two objects ( sam and bob ) of the same class ( Car ), and we have used the car attributes outside the Class block using its object and dot operator.
Class Methods:
Functions inside a Class know as class methods. There are two types of Class methods user-defined methods and magical methods. The python magical methods also are known as under these methods contain double underscores (__) before and after the method name.
Example:
#Output
Python Object-Oriented Programming Properties:
1. Inheritance
Inheritance is one of the most important properties of OPP’s concept and it used more often as compared to other OOP’s properties. In inheritance, we can use the properties ( Attributes and Method) of one class in another. The main motto of inheritance is the reusability, code written in one class can be used in another. Inheritance has two components: base class and derived class. The Base class also known as Parent class and the derived also know as child class. The derived class inherit the properties from the base class.
Syntax
Example:
#Output:
2. Encapsulation
Encapsulation deal with data hiding from an Object. In python class, we use single or double underscore before the attributes name to tell the object that particular attribute is private for class only and object not supposed to access those attributes directly.
Example:
#Output:
3. Polymorphism
With polymorphism, we can have the same method name for different classes. Polymorphism applies on operators too for example if we use + operator between two integers it will add both integers, but if we use the same + operator between two string it will concatenate them.
Example:
#Output:
Some Advantages of OOPs:
- OOPs provide a modular way to complete the project
- It increases the code reusability
- Give more data security.